package com.dlc.shop.combo.multishop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dlc.shop.bean.model.GiveawayProd;
import com.dlc.shop.bean.model.Product;
import com.dlc.shop.combo.multishop.service.GiveawayProdService;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.security.multishop.util.SecurityUtils;
import com.dlc.shop.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 套装商品项
 *
 * @author LGH
 * @date 2021-11-08 13:29:16
 */
@RestController
@AllArgsConstructor
@RequestMapping("/shop/giveawayProd" )
@Tag(name = "赠品商品项接口")
public class GiveawayProdController {

    private final GiveawayProdService giveawayProdService;

    private final ProductService productService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param giveawayProd 套装商品项
     * @return 分页数据
     */
    @GetMapping("/page" )
    @Operation(summary = "分页获取赠品信息" , description = "分页获取赠品信息")
    @PreAuthorize("@pms.hasPermission('shop:giveawayProd:update')" )
    public ServerResponseEntity<IPage<GiveawayProd>> getGiveawayProdPage(PageParam<GiveawayProd> page, GiveawayProd giveawayProd) {
        return ServerResponseEntity.success(giveawayProdService.page(page, new LambdaQueryWrapper<GiveawayProd>()));
    }

    /**
     * 主赠送商品分页查询
     * @param page 分页对象
     * @param giveawayProd 套装商品项
     * @return 分页数据
     */
    @GetMapping("/mainProdPage" )
    @Operation(summary = "分页获取主赠送商品" , description = "分页获取赠品信息")
    public ServerResponseEntity<IPage<Product>> getMainProdPage(PageParam<Product> page, GiveawayProd giveawayProd) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        giveawayProd.setShopId(shopId);
        IPage<Product> mainProdPage = giveawayProdService.getMainProdPage(page, giveawayProd);
        productService.loadProdStock(mainProdPage.getRecords());
        return ServerResponseEntity.success(mainProdPage);
    }

}
